import { getFnInputParam } from '@/utils/hyurl'
import { getFormModelKeyData } from './hyform'
import { getTplKeyObj } from './hyctr'
import {evalEl} from "./auth";
import {showActiveListKey} from "./hylist";

export function clickTabsCallBack(thisObj, key) {
  if (key === 'tabs01') {
    clickTabsCallBack01(thisObj, key)
  } else if (key === 'tabs02') {
    clickTabsCallBack02(thisObj, key)
  } else if (key === 'tabs03') {
    clickTabsCallBack03(thisObj, key)
  } else if (key === 'tabs04') {
    clickTabsCallBack04(thisObj, key)
  }
}

export function clickTabsCallBack01(thisObj, key, globalData) {
  if (key !== undefined) {
    thisObj.tabListActiveKey01 = key
    thisObj.$nextTick(() => {
      changeActiveTabData01(thisObj, globalData)
    })
  }
}
export function clickTabsCallBack02(thisObj, key, globalData) {
  if (key !== undefined) {
    thisObj.tabListActiveKey02 = key
    thisObj.$nextTick(() => {
      changeActiveTabData02(thisObj, globalData)
    })
  }
}
export function clickTabsCallBack03(thisObj, key, globalData) {
  if (key !== undefined) {
    thisObj.tabListActiveKey03 = key
    thisObj.$nextTick(() => {
      changeActiveTabData03(thisObj, globalData)
    })
  }
}
export function clickTabsCallBack04(thisObj, key, globalData) {
  if (key !== undefined) {
    thisObj.tabListActiveKey04 = key
    thisObj.$nextTick(() => {
      changeActiveTabData04(thisObj, globalData)
    })
  }
}

export function changeActiveTabData(thisObj, globalData) {
  changeActiveTabData01(thisObj, globalData)
  changeActiveTabData02(thisObj, globalData)
  changeActiveTabData03(thisObj, globalData)
}
export function changeActiveTabData01(thisObj, globalData) {
  const activeTab = getActiveTabData01(thisObj)
  changeActiveTabCommonData(thisObj, thisObj.tabListActiveKey01, activeTab, globalData)
}
export function changeActiveTabData02(thisObj, globalData) {
  const activeTab = getActiveTabData02(thisObj)
  changeActiveTabCommonData(thisObj, thisObj.tabListActiveKey02, activeTab, globalData)
}
export function changeActiveTabData03(thisObj, globalData) {
  const activeTab = getActiveTabData03(thisObj)
  changeActiveTabCommonData(thisObj.tabListActiveKey03, activeTab, globalData)
}
export function changeActiveTabData04(thisObj, globalData) {
  const activeTab = getActiveTabData04(thisObj)
  changeActiveTabCommonData(thisObj, thisObj.tabListActiveKey04, activeTab, globalData)
}
export function changeActiveTabCommonData(thisObj, tabsKey, activeTab, globalData) {
  if (thisObj === null) {
    return
  }
  if (tabsKey !== null) {
    thisObj.$nextTick(() => {
      const tplKey = activeTab.activeTplKey
      const formKeyData = getFormModelKeyData(thisObj)
      const tmpGlobalData = {}
      if (undefined !== globalData) {
        tmpGlobalData.openParam = globalData.globalParam
        tmpGlobalData.globalParam = globalData.globalParam
        tmpGlobalData.formModel = formKeyData
      } else {
        const mergeParam = Object.assign({}, thisObj.initData, thisObj.openParam, formKeyData)
        tmpGlobalData.openParam = mergeParam
        tmpGlobalData.globalParam = mergeParam
        tmpGlobalData.formModel = formKeyData
      }
      const param = getFnInputParam(thisObj, tmpGlobalData, activeTab.active.refSetOpenVal)
      // const param = getFnInputParam(globalData, activeTab.active.refSetOpenVal)

      console.debug('tab 切换', tplKey, param)
      if (thisObj.$refs[tplKey] !== undefined) {
        const tplKeyObj = getTplKeyObj(thisObj, tplKey)
        tplKeyObj.changeAppContainnerCssObj()
        const tmpInitSrcObj = tplKeyObj.initSrcObj
        const rowsList = thisObj.globalData[tplKey]
        if (rowsList !== undefined && rowsList.length > 0) {
          tplKeyObj.initSrcObj.initSrcSearch = false
        }
        tmpInitSrcObj.initSrcLabel = thisObj.initSrcObj.initSrcLabel
        if (activeTab.active.S_REF_PANEL_TYPE !== undefined) {
          tmpInitSrcObj.initSrcType = activeTab.active.S_REF_PANEL_TYPE
        }
        console.log('tmpInitSrcObj',tmpInitSrcObj)
        thisObj.changeTableUi(tplKey, param, tmpInitSrcObj)
      }
    })
  }
}
export function getActiveTabData01(thisObj) {
  if (thisObj.tabList01 !== undefined) {
    for (const i in thisObj.tabList01) {
      const tab = thisObj.tabList01[i]
      if (thisObj.tabListActiveKey01 === tab.key) {
        return tab
      }
    }
    return thisObj.tabList01[0]
  } else {
    return null
  }
}
export function getActiveTabData02(thisObj) {
  if (thisObj.tabList02 !== undefined) {
    for (const i in thisObj.tabList02) {
      const tab = thisObj.tabList02[i]
      if (thisObj.tabListActiveKey02 === tab.key) {
        return tab
      }
    }
    return thisObj.tabList02[0]
  } else {
    return null
  }
}
export function getActiveTabData03(thisObj) {
  if (thisObj.tabList03 !== undefined) {
    for (const i in thisObj.tabList03) {
      const tab = thisObj.tabList03[i]
      if (thisObj.tabListActiveKey03 === tab.key) {
        return tab
      }
    }
    return thisObj.tabList03[0]
  } else {
    return null
  }
}
export function getActiveTabData04(thisObj) {
  if (thisObj.tabList04 !== undefined) {
    for (const i in thisObj.tabList04) {
      const tab = thisObj.tabList04[i]
      if (thisObj.tabListActiveKey04 === tab.key) {
        return tab
      }
    }
    return thisObj.tabList04[0]
  } else {
    return null
  }
}
export function changeTabListActiveKey(thisObj) {
  changeTabListActiveKey01(thisObj)
  changeTabListActiveKey02(thisObj)
  changeTabListActiveKey03(thisObj)
  changeTabListActiveKey04(thisObj)
}
export function changeTabListActiveKey01(thisObj) {
  if (thisObj.tabList01 !== undefined && thisObj.tabList01.length > 0) {
    thisObj.tabListActiveKey01 = thisObj.tabList01[0].key
  }
}
export function changeTabListActiveKey02(thisObj) {
  if (thisObj.tabList02 !== undefined && thisObj.tabList02.length > 0) {
    thisObj.tabListActiveKey02 = thisObj.tabList02[0].key
  }
}
export function changeTabListActiveKey03(thisObj) {
  if (thisObj.tabList03 !== undefined && thisObj.tabList03.length > 0) {
    thisObj.tabListActiveKey03 = thisObj.tabList03[0].key
  }
}
export function changeTabListActiveKey04(thisObj) {
  if (thisObj.tabList04 !== undefined && thisObj.tabList04.length > 0) {
    thisObj.tabListActiveKey04 = thisObj.tabList04[0].key
  }
}
export function initTabList(thisObj) {
  if (thisObj.initUiVal.tabs !== undefined && thisObj.initUiVal.tabs.tabsKey !== undefined) {
    initTab01List(thisObj)
    initTab02List(thisObj)
    initTab03List(thisObj)
    initTab04List(thisObj)
  }
}

export function showActiveTabKey(thisObj, S_RULE_TPL_KEY, globalData) {
  changeTab01List(thisObj)
}

export function showHiddenTabKey(thisObj, S_RULE_TPL_KEY, globalData) {
  showHiddenTab01Key(thisObj)
}

export function showHiddenTab01Key(thisObj, S_RULE_TPL_KEY, globalData) {
  const tmpTabList = []
  for (const tmpTabIdx in thisObj.tabList01) {
    const tmpTabData = tmpTabList[tmpTabIdx]
    if (S_RULE_TPL_KEY !== tmpTabData.key){
      tmpTabList.push(tmpTabData)
    }
  }
  thisObj.tabList01 = tmpTabList
}

export function changeTab01List(thisObj) {
  if (thisObj.initUiVal.tabs !== undefined && thisObj.initUiVal.tabs[thisObj.tabsKey01] !== undefined) {
    const tmpTabList = thisObj.initUiVal.tabs[thisObj.tabsKey01].tabList
    thisObj.tabList01 = []
    for (const tmpTabIdx in tmpTabList) {
      const tmpTabData = tmpTabList[tmpTabIdx]
      if (evalEl(tmpTabData.objEl, thisObj.globalData.globalParam)) {
        thisObj.tabList01.push(tmpTabData)
      }
    }
  }
}
export function initTab01List(thisObj) {
  if (thisObj.initUiVal.tabs !== undefined && thisObj.initUiVal.tabs[thisObj.tabsKey01] !== undefined) {
    const tmpTabList = thisObj.initUiVal.tabs[thisObj.tabsKey01].tabList
    if (tmpTabList.length > 0) {
      thisObj.tabsShow01 = true
      thisObj.formTabShow = true
    }
    thisObj.tabList01 = []
    for (const tmpTabIdx in tmpTabList) {
      const tmpTabData = tmpTabList[tmpTabIdx]
      thisObj.tabList01.push(tmpTabData)
    }
  }
}
export function initTab02List(thisObj) {
  if (thisObj.initUiVal.tabs !== undefined && thisObj.initUiVal.tabs[thisObj.tabsKey02] !== undefined) {
    thisObj.tabList02 = thisObj.initUiVal.tabs[thisObj.tabsKey02].tabList
    if (thisObj.tabList02 !== undefined) {
      thisObj.tabsShow02 = true
      thisObj.formTabShow = true
    }
  }
}
export function initTab03List(thisObj) {
  if (thisObj.initUiVal.tabs !== undefined && thisObj.initUiVal.tabs[thisObj.tabsKey03] !== undefined) {
    thisObj.tabList03 = thisObj.initUiVal.tabs[thisObj.tabsKey03].tabList
    if (thisObj.tabList03 !== undefined) {
      thisObj.tabsShow03 = true
      thisObj.formTabShow = true
    }
  }
}
export function initTab04List(thisObj) {
  if (thisObj.initUiVal.tabs !== undefined && thisObj.initUiVal.tabs[thisObj.tabsKey04] !== undefined) {
    thisObj.tabList04 = thisObj.initUiVal.tabs[thisObj.tabsKey04].tabList
    if (thisObj.tabList04 !== undefined) {
      thisObj.tabsShow04 = true
      thisObj.formTabShow = true
    }
  }
}
